package com.gcsoftware.web.controller;

import com.gcsoftware.exception.ControllerException;
import com.gcsoftware.web.beans.returnPage.ApiCount;
import com.gcsoftware.web.beans.returnPage.AppCountBar;
import com.gcsoftware.web.beans.returnPage.AsensitiveItem;
import com.gcsoftware.web.beans.returnPage.PageObject;
import com.gcsoftware.web.mapper.T_GW_DSITEMMAPPER;
import com.gcsoftware.web.mapper.elasticsearch.ReportDao;
import com.gcsoftware.web.service.elasticsearch.HomeService;
import com.gcsoftware.web.service.elasticsearch.ReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Set;

/**
 * 报表
 * @author czp
 * date : 2018-04-10
 */
@Controller
@RequestMapping("/report")
public class ReportController extends BaseController {
    @Autowired
    private HomeService homeService;

    @Autowired
    private ReportService reportService;

    @Autowired
    private ReportDao reportDao;

    @Autowired
    private T_GW_DSITEMMAPPER t_gw_dsitemmapper;

    /**
     * 进入API统计
     * @return 页面
     * curl http://localhost:8080/report/toApiCountPage -X GET
     */
    @RequestMapping("/toApiCountPage")
    public String toApiCountPage() {
        logger.info(" to api count page ... ");
        return "admin/api-report";
    }

    /**
     * 进入敏感项统计
     * @return 页面
     * curl http://localhost:8080/report/toSensitiveItemsPage -X GET
     */
    @RequestMapping("/toSensitiveItemsPage")
    public String toSensitiveItemsPage() {
        logger.info(" to sensitive items count page ... ");
        return "admin/sensitive-item-report";
    }

    /**
     * 接口统计报表1
     * @return 报表数据
     * curl http://localhost:8080/report/apiCountReport -X POST -d "rows=10&page=1"
     */
    @ResponseBody
    @RequestMapping(value = "/apiCountReport", method = RequestMethod.POST)
    public PageObject<ApiCount> apiCountReport(@RequestParam("rows") int rows,
        @RequestParam("page") int page,
        @RequestParam(value = "appName", required = false) String appName,
        @RequestParam(value = "apiName", required = false) String apiName,
        @RequestParam(value = "sord", required = false) String sord,
        @RequestParam(value = "time_range", required = false) String range) {
            if (PageParameterCheck(page, rows)) {
                return reportService.apiCountReport(rows, page, appName, apiName, PageSort(sord), range);
            } else {
                throw new ControllerException(" apiCountReport() ");
            }
    }

    /**
     * 接口统计报表2
     * @return 报表数据
     * curl http://localhost:8080/report/apiCountRep -X POST
     */
    @ResponseBody
    @RequestMapping(value = "/apiCountRep", method = RequestMethod.POST)
    public AppCountBar apiCountRep(@RequestParam(value = "appName", required = false) String appName,
       @RequestParam(value = "apiName", required = false) String apiName,
       @RequestParam(value = "time_range", required = false) String range) {
            return homeService.apiCountBar(range, appName, apiName);
    }

    /**
     * 敏感项统计报表
     * @return 报表数据
     * curl http://localhost:8080/ds/report/asensitiveItemCountReport -X POST
     */
    @ResponseBody
    @RequestMapping(value = "/asensitiveItemCountReport", method = RequestMethod.POST)
    public List<AsensitiveItem> asensitiveItemCountReport(
        @RequestParam(value = "asensitiveItem", required = false) String asensitiveItem,
        @RequestParam(value = "time_range", required = false) String range) {
            return reportService.asensitiveItemCountReport(asensitiveItem, range);
    }

    /**
     * 单个敏感项保护统计
     * @return 图表数据
     * curl http://localhost:8080/report/asensitiveItemCharts -X POST
     */
    @ResponseBody
    @RequestMapping(value = "/asensitiveItemCharts", method = RequestMethod.POST)
    public Object asensitiveItemCharts(@RequestParam(value = "itemName", required = false) String asensitiveItem,
        @RequestParam(value = "time_range", required = false) String range) {
            logger.info("\nrange : " + range + "\nasensitiveItem" + asensitiveItem);
            return reportDao.asensitiveItemCharts(asensitiveItem, range);
    }

    /**
     * API & APP 统计图
     * @return 图表数据
     * curl http://localhost:8080/report/apiCountCharts -X POST
     */
    @ResponseBody
    @RequestMapping(value = "/apiCountCharts", method = RequestMethod.POST)
    public Object apiCountCharts(@RequestParam(value = "appName", required = false) String appName,
       @RequestParam(value = "apiName", required = false) String apiName,
       @RequestParam(value = "time_range", required = false) String range) {
           return reportDao.apiCountCharts(appName, apiName, range);
    }

    /**
     * 获取API列表
     * @return 列表
     * curl http://localhost:8080/report/getApiNames -X POST
     */
    @ResponseBody
    @RequestMapping(value = "/getApiNames", method = RequestMethod.POST)
    public Set getApiNames() {
          return reportService.getApiNames();
    }

    /**
     * 获取APP列表
     * @return 列表
     * curl http://localhost:8080/report/getAppName -X POST
     */
    @ResponseBody
    @RequestMapping(value = "/getAppNames", method = RequestMethod.POST)
    public Set getAppNames() {
        return reportService.getAppNames();
    }

    /**
     * 获取敏感项列表
     * @return 列表
     * curl http://loclhost:8080/report/getSensitivityItems -X POST
     */
    @ResponseBody
    @RequestMapping(value = "/getSensitivityItems", method = RequestMethod.POST)
    public List<String> getSensitivityItems() {
        return t_gw_dsitemmapper.getSensitivityItems();
    }
}
